home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / test / componen.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  7.5 KB  |  307 lines

  1. /*
  2.  * @(#)ComponentTest.java    1.31 95/09/01 Sami Shaio
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. import java.awt.*;
  21. import java.util.Date;
  22.  
  23. public class ComponentTest extends Frame {
  24.     Panel     center;
  25.     QuitDialog    quitDialog;
  26.     AboutBox    aboutBox;
  27.     FileDialog    openDialog;
  28.     FileDialog    saveDialog;
  29.  
  30.     public ComponentTest() {
  31.     aboutBox = new AboutBox(this);
  32.     quitDialog = new QuitDialog(this);
  33.     openDialog = new FileDialog(this, "Open File...");
  34.     saveDialog = new FileDialog(this, "Save File...", FileDialog.SAVE);
  35.     setTitle("ComponentTest");
  36.     setResizable(false);
  37.     setBackground(Color.lightGray);
  38.     setFont(new Font("Helvetica", Font.PLAIN, 12));
  39.     setLayout(new BorderLayout());
  40.  
  41.     MenuBar    mb = new MenuBar();
  42.     Menu   m = new Menu("File");
  43.  
  44.     m.add(new MenuItem("About ComponentTest"));
  45.     m.add(new MenuItem("Open..."));
  46.     m.add(new MenuItem("Save..."));
  47.     m.add(new MenuItem("Quit"));
  48.     mb.add(m);
  49.     setMenuBar(mb);
  50.  
  51.     Label l  = new Label("North");
  52.     l.setAlignment(Label.CENTER);
  53.  
  54.     add("South", new TextField("South"));
  55.     add("North", l);
  56.     center = new ScrollPanel();
  57.     center.setLayout(new BorderLayout());
  58.     add("Center", center);
  59.  
  60.     Panel innerPanel = new ScrollPanel();
  61.     center.add("Center", innerPanel);
  62.     innerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
  63.     innerPanel.add(new Label("List of Widgets"));
  64.     innerPanel.add(new Button("Arthur"));
  65.     innerPanel.add(new Button("Sami"));
  66.     innerPanel.add(new SillyWidget());
  67.     innerPanel.add(new TextField("quite random"));
  68.     innerPanel.add(new Scrollbar(Scrollbar.VERTICAL));
  69.     innerPanel.add(new Scrollbar(Scrollbar.HORIZONTAL));
  70.  
  71.     List li = new List(4, false);
  72.  
  73.     li.reshape(0, 0, 100, 100);
  74.     li.addItem("Arthur");
  75.     li.addItem("Sami");
  76.     li.addItem("James");
  77.     li.addItem("Chris");
  78.     li.addItem("Mary");
  79.     li.addItem("Lisa");
  80.     li.addItem("Kathy");
  81.     li.select(1);
  82.  
  83.     innerPanel.add(li);
  84.     TextArea tx = new TextArea(10, 5);
  85.     tx.setText("this is some random text");
  86.     tx.setForeground(Color.red);
  87.  
  88.     innerPanel.add(tx);
  89.  
  90.     Choice    opt = new Choice();
  91.  
  92.     opt.addItem("Arthur");
  93.     opt.addItem("Sami");
  94.     opt.addItem("Chris");
  95.     opt.addItem("Jim");
  96.  
  97.     innerPanel.add(opt);
  98.  
  99.     CheckboxGroup g = new CheckboxGroup();
  100.     innerPanel.add(new Checkbox("one", g, false));
  101.     innerPanel.add(new Checkbox("two", g, true));
  102.     innerPanel.add(new Checkbox("three", g, false));
  103.     center.add("East", new Scrollbar(Scrollbar.VERTICAL));
  104.     center.add("South", new Scrollbar(Scrollbar.HORIZONTAL));
  105.  
  106.     add("East", new Button("East"));
  107.     add("West", new Button("West"));
  108.     resize(300, 500);
  109.     show();
  110.     }
  111.         
  112.     public synchronized boolean handleEvent(Event e) {
  113.     if (e.id == Event.WINDOW_DESTROY) {
  114.         quitDialog.show();
  115.         return true;
  116.     } else {
  117.         return super.handleEvent(e);
  118.     }
  119.     }
  120.  
  121.     public synchronized void wakeUp() {
  122.     notify();
  123.     }
  124.  
  125.     public boolean action(Event evt, Object obj) {
  126.     if (evt.target instanceof MenuItem) {
  127.         String label = (String)obj;
  128.  
  129.         if (label.equals("Quit")) {
  130.         quitDialog.show();
  131.         } else if (label.equals("About ComponentTest")) {
  132.         Rectangle bounds = bounds();
  133.         Rectangle abounds = aboutBox.bounds();
  134.         aboutBox.move(bounds.x + (bounds.width - abounds.width)/ 2,
  135.                   bounds.y + (bounds.height - abounds.height)/2);
  136.         aboutBox.show();
  137.         } else if (label.equals("Open...")) {
  138.         openDialog.setDirectory("/tmp");
  139.         openDialog.show();
  140.         if (openDialog.getFile() != null) {
  141.             System.out.println("OPEN: " + openDialog.getFile());
  142.         }
  143.         } else if (label.equals("Save...")) {
  144.         saveDialog.show();
  145.         if (saveDialog.getFile() != null) {
  146.             System.out.println("SAVE: " + saveDialog.getFile());
  147.         }
  148.         }
  149.     }
  150.     return true;
  151.     }
  152.  
  153.     public static void main(String args[]) {
  154.     new ComponentPrintTest(new ComponentTest());
  155.     }
  156. }
  157.  
  158. class ComponentPrintTest extends Frame {
  159.     Component comp;
  160.  
  161.     public ComponentPrintTest(Component comp) {
  162.     this.comp = comp;
  163.     reshape(400, 0, 400, 400);
  164.     show();
  165.     }
  166.  
  167.     public void paint(Graphics g) {
  168.     Dimension d = size();
  169.     g.setColor(Color.green);
  170.     g.fillRect(0, 0, d.width, d.height);
  171.     g.setColor(Color.black);
  172.     comp.printAll(g);
  173.     }
  174. }
  175.  
  176. class ScrollPanel extends Panel {
  177.     public ScrollPanel() {
  178.     reshape(0, 0, 100, 100);
  179.     }
  180.  
  181.     public void paint(Graphics g) {
  182.     Rectangle r = bounds();
  183.  
  184.     g.drawRect(0, 0, r.width, r.height);
  185.     }
  186. }
  187.  
  188. class SillyWidget extends Canvas {
  189.     Color    c1 = Color.pink;
  190.     Color    c2 = Color.lightGray;
  191.     long    entered = 0;
  192.     long    inComponent = 0;
  193.     Font    font = new Font("Courier", Font.BOLD, 12);
  194.     FontMetrics fm;
  195.  
  196.     public SillyWidget() {
  197.     reshape(0, 0, 100, 100);
  198.     }
  199.  
  200.     public boolean mouseEnter(Event e, int x, int y) {
  201.     Color c = c1;
  202.     entered = e.when;
  203.     c1 = c2;
  204.     c2 = c;
  205.     repaint();
  206.     return true;
  207.     }
  208.  
  209.     public boolean mouseExit(Event e, int x, int y) {
  210.     Color c = c1;
  211.  
  212.     inComponent = e.when - entered;
  213.     c1 = c2;
  214.     c2 = c;
  215.     repaint();
  216.     return true;
  217.     }
  218.  
  219.     public void paint(Graphics g) {
  220.     Rectangle    r = bounds();
  221.  
  222.     g.setColor(c1);
  223.     g.fillRoundRect(0, 0, r.width, r.height, 20, 20);
  224.     g.setColor(c2);
  225.     g.fillRoundRect(5, 5, r.width - 10, r.height - 10, 20, 20);
  226.     g.setColor(c1);
  227.     g.fillRoundRect(15, 15, r.width - 30, r.height - 30, 20, 20);
  228.     g.setColor(Color.darkGray);
  229.     g.setFont(font);
  230.     fm = g.getFontMetrics(font);
  231.     String s = ""+inComponent+" ms.";
  232.     int width = fm.stringWidth(s);
  233.     System.out.println("width = " + width);
  234.     g.drawString(s,
  235.              (r.width - width) / 2,
  236.              (r.height - fm.getHeight()) / 2);
  237.     }
  238. }
  239.     
  240. class AboutBox extends Window {
  241.     public AboutBox(Frame parent) {
  242.     super(parent);
  243.     setBackground(new Color(100,200, 200));
  244.     add("Center", new Label("ComponentTest",Label.CENTER));
  245.     Panel p = new Panel();
  246.     add("South", p);
  247.     p.add(new Button("ok"));
  248.     reshape(0, 0, 200, 100);
  249.     }
  250.     public void paint(Graphics g) {
  251.     Rectangle bounds = bounds();
  252.  
  253.     g.setColor(getBackground());
  254.     g.draw3DRect(0, 0, bounds.width - 1, bounds.height - 1, true);
  255.     }
  256.     public Insets insets() {
  257.     return new Insets(5, 5, 5, 5);
  258.     }
  259.     public boolean action(Event e, Object arg) {
  260.     hide();
  261.     return true;
  262.     }
  263. }
  264.  
  265. class QuitDialog extends Dialog {
  266.     public QuitDialog(Frame parent) {
  267.     super(parent, "Quit Application?", true);
  268.     setLayout(new FlowLayout());
  269.     add(new Button("Yes"));
  270.     add(new Button("No"));
  271.     reshape(30, 30, 200, 50);
  272.     setResizable(false);
  273.     }
  274.  
  275.     public synchronized void show() {
  276.     Rectangle bounds = getParent().bounds();
  277.     Rectangle abounds = bounds();
  278.     move(bounds.x + (bounds.width - abounds.width)/ 2,
  279.          bounds.y + (bounds.height - abounds.height)/2);
  280.     super.show();
  281.     }
  282.  
  283.     public synchronized void wakeUp() {
  284.     notify();
  285.     }
  286.  
  287.     public boolean handleEvent(Event e) {
  288.     if (e.id == Event.WINDOW_DESTROY) {
  289.         hide();
  290.         return true;
  291.     } else {
  292.         return super.handleEvent(e);
  293.     }
  294.     }
  295.  
  296.     public boolean action(Event e, Object arg) {
  297.     String label = (String)arg;
  298.  
  299.     if (label.equals("Yes")) {
  300.         System.exit(0);
  301.     } else if (label.equals("No")) {
  302.         hide();
  303.     }
  304.     return true;
  305.     }
  306. }
  307.